package com.py.credit.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.py.credit.common.ServiceException;
import com.py.credit.dao.RuleBindingDao;
import com.py.credit.dao.TableRuleDao;
import com.py.credit.entity.common.InspectRule;
import com.py.credit.service.TableRuleService;
import com.py.credit.utils.SQLBuilder;
import com.py.framework.core.common.Page;

@Service
public class TableRuleServiceImpl implements TableRuleService {

	@Autowired
	private TableRuleDao tableRuleDao;

	@Autowired
	private RuleBindingDao ruleBindingDao;

	/**
	 * 添加表规则
	 */
	@Override
	public void add(String ruleName, String ruleDesc, String rulePackage, String ruleMethod, Long managerId,
			String time, String type,String ruleMsg) {
		tableRuleDao.add(ruleName, ruleDesc, rulePackage, ruleMethod, managerId, time, type,ruleMsg);
	}

	/**
	 * 批量删除表规则
	 */
	@Override
	public void del(String ids) {
		int total = ruleBindingDao.selectTbaRelationTotal(Arrays.asList(ids.split(",")));
		if (total > 0) {
			throw new ServiceException("规则有绑定关系，不能删除！");// 抛出异常
		}
		tableRuleDao.delete(Arrays.asList(ids.split(",")));
	}

	/**
	 * 获取单个数据
	 */
	@Override
	public InspectRule select(int id) {
		return tableRuleDao.select(id);
	}

	/**
	 * 修改表规则
	 */
	@Override
	public void modify(int id, String ruleName, String ruleDesc, String rulePackage, String ruleMethod, Long managerId,
			String time,String ruleMsg) {
		tableRuleDao.modify(id, ruleName, ruleDesc, rulePackage, ruleMethod, managerId, time,ruleMsg);
	}

	@Override
	public int selectSameName(String ruleName) {
		// TODO Auto-generated method stub
		return tableRuleDao.selectSameName(ruleName);
	}

	/**
	 *
	 * @Function 分页查询表规则数据
	 * @FileName TableRuleServiceImpl.java
	 * @MethodName getByConditionForPage
	 * @param columns
	 * @param additionalColumns
	 * @param conditionStr
	 * @param page
	 * @param tableId
	 * @return
	 * @Date 2018年7月26日 下午5:02:06
	 * @Author Min DongXv
	 *
	 */
	@Override
	public List<Map<String, Object>> getByConditionForPage(List<String> columns, String[] additionalColumns,
			String conditionStr, Page page, Long tableId) {
		List<String> conditions = SQLBuilder.createConditionSql(conditionStr);
		Integer version = ruleBindingDao.selectCurrentContentVersion();
		List<Map<String, Object>> batchLogs = null;
		if (version != null && version > 0) {
			batchLogs = tableRuleDao.selectByConditionForPage(columns, additionalColumns, conditions, page, tableId,
					version);
		} else {
			batchLogs = tableRuleDao.selectNoVersionByConditionForPage(columns, additionalColumns, conditions, page);
		}
		return batchLogs;
	}

	@Override
	@Deprecated
	public List<Map<String, Object>> getByCondition(String condition, List<String> columns) {
		List<String> conditions = SQLBuilder.createConditionSql(condition);

		return tableRuleDao.selectByCondition(columns, conditions);
	}

	@Override
	public List<InspectRule> getTableRules(String ruleName) {

		return tableRuleDao.selectTableRules(ruleName);
	}

	@Override
	public List<InspectRule> getBindRuleTables(Integer version, String ruleId, String resourceName, Page page) {
		return tableRuleDao.selectBindRuleTablesForPage(version, ruleId, resourceName, page);

	}

}
